package main.java.indi.zyj.hot100;

class MinStack {

    Node root;
    public MinStack() {
        this.root = new Node();
    }

    public void push(int val) {
        Node node = new Node(val, root.next);
        node.min = root.min;
        root.next = node;
        root.min = Math.min(root.min, val);
    }

    public void pop() {
        Node cur = root.next;
        if (cur.val == root.min) {
            root.min = cur.min;
        }
        root.next = cur.next;
        cur.next = null;
    }

    public int top() {
        return root.next.val;
    }

    public int getMin() {
        return root.min;
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(val);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */